import { guid } from "@/utils";
import { emitter } from "@/utils/mitt";
import { clearAllStats } from "@/views/associationAnalysis/canvas/components/canvasG6/common/index";
import { graph } from "./index";
// import { nodeOptions } from "@/views/buildKg/ontology/common/state";
import { delVerifyApi } from "@/api/buildKg/tology";
import { ElMessage } from "element-plus";
import { useCanvasStoreHook } from "@/stores/modules/canvas"; // 画布
import { useSelectGraphStoreHook } from "@/stores/modules/selectGraph"; // 画布

const useSelectGraphStore = useSelectGraphStoreHook(); // 画布
const useCanvasStore = useCanvasStoreHook(); // 画布

export const isNeedStop = ref(false);

// export const handleCopyObject = (ev) => {
//   setAllStateDark();
//   const model = ev.item.getModel();
//   graph.addItem(
//     "node",
//     {
//       ...model,
//       id: guid(),
//       label: `object-${guid()}`,
//       x: model.x + 10,
//       y: model.y + 10,
//     },
//     true
//   );
// };

export const handleDeleteObject = async () => {
  const selectDatas = useSelectGraphStore.getSelectCanvas();
  const canvasData = {
    // 现在图中的所有节点
    nodes: graph.getNodes().map((item) => item.getModel()),
    edges: graph.getEdges().map((item) => item.getModel()),
  };
  const nodeids = selectDatas.nodes.map((item) => item.id); // 选中的节点
  const edgeids = selectDatas.edges.map((item) => item.id); // 选中的边
  // selectDatas.edges.forEach((item) => {
  //   // 通过 ID 查询节点实例
  //   const node = graph.findById(item.id);
  //   graph.removeItem(node);
  // });
  canvasData.nodes = canvasData.nodes.filter(
    // 去掉选中的节点
    (item) => !nodeids.includes(item.id)
  );
  canvasData.edges = canvasData.edges.filter(
    // 去掉选中的节点所关联的边及选中的边
    (item) =>
      !nodeids.includes(item.target) &&
      !nodeids.includes(item.source) &&
      !edgeids.includes(item.id)
  );
  graph.changeData(canvasData); // 重新渲染去掉选中后的节点以及边后的数据，撤销的时候才是一次性撤销所有批量操作，循环删除节点撤销只能撤销一个点
  isNeedStop.value = true;
  useSelectGraphStore.setSelectCanvas(null);
  useCanvasStore.setCanvas(JSON.parse(JSON.stringify(canvasData))); // 边添加到画布
};

export const handleAddDimension = (currentEvt) => {
  console.log(currentEvt);
  graph.setItemState(currentEvt.item, "selected", true);
  const allSelectNode = graph.findAllByState("node", "selected"); // 获取所有选中状态的节点
  const allSelectEdge = graph.findAllByState("edge", "selected"); // 获取所有选中状态的边

  const data = {
    nodes: allSelectNode.map((item) => item._cfg.model),
    edges: allSelectEdge.map((item) => item._cfg.model),
  };

  useSelectGraphStore.setSelectCanvas(data); // 选中节点存到store
  emitter.emit("dimensionEvent", "add");
};

export const handleHandleInvert = () => {
  emitter.emit("handleInvert");
};

/** 选中关联节点 */
export const handleChooseAssociation = (currentEvt) => {
  let neighborsNodes = []; // 获取当前节点的所有连接节点
  graph.setItemState(currentEvt.item, "selected", true);
  const allSelectNode = graph.findAllByState("node", "selected"); // 获取所有选中状态的节点
  const allSelectEdge = graph.findAllByState("edge", "selected"); // 获取所有选中状态的边
  allSelectNode.forEach((item) => {
    //找出每个选中节点所有有关系的节点
    neighborsNodes = neighborsNodes.concat(
      Array.from(new Set(graph.getNeighbors(item)))
    );
  });
  // currentEvt.item.setState("selected", true);
  console.log(neighborsNodes, "neighbors", allSelectNode);
  neighborsNodes.forEach((it) => {
    // 选中节点所有有关系的节点全部选中
    graph.setItemState(it, "selected", true);
  });

  const data = {
    nodes: neighborsNodes.concat(allSelectNode).map((item) => item._cfg.model),
    edges: allSelectEdge.map((item) => item._cfg.model),
  };

  useSelectGraphStore.setSelectCanvas(data); // 选中节点存到store
};

// export const handleExpand = () => {
//   clearAllStats();
//   graph.getNodes().forEach((node) => {
//     graph.setItemState(node, "dark", true);
//   });
//   graph.getEdges().forEach((edge) => {
//     graph.setItemState(edge, "selected", true);
//   });
// };

// export const handleAllChoose = () => {
//   clearAllStats();
//   graph.getNodes().forEach((node) => {
//     graph.setItemState(node, "highlight", true);
//   });
//   graph.getEdges().forEach((edge) => {
//     graph.setItemState(edge, "selected", true);
//   });
// };

// export const handleBuildObject = (ev) => {
//   // Add a new node
//   const rand = Math.floor(Math.random() * 10);
//   graph.addItem("node", {
//     x: ev.x,
//     y: ev.y,
//     id: guid(), // Generate the unique id
//     label: `object_${guid()}`,
//     isAdd: true,
//     style: {
//       fill: colorList[rand],
//     },
//     labelCfg: {
//       style: {
//         fill: colorList[rand],
//       },
//     },
//     stateStyles: nodeOptions({ backgroundColor: colorList[rand] }),
//   });
// };
